/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bean;

import ejb.Account;
import ejb.AccountFacadeLocal;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author longdm0708l
 */
public class AccountBean {
    public void createAccount(Account account)
    {
        lookupAccountFacade().create(account);
    }

    public void editAccount(Account account)
    {
        lookupAccountFacade().edit(account);
    }

    public void removeAccount(Account account)
    {
        lookupAccountFacade().remove(account);
    }

    private String loginName;

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public List<Account> getAllAccount()
    {
        return lookupAccountFacade().findAll();
    }

    public Account getAccountByLoginName()
    {
        return lookupAccountFacade().find(getLoginName());
    }

    public int checkLogin(String userName, String password){
        return lookupAccountFacade().checkLogin(userName, password);
    }

    private AccountFacadeLocal lookupAccountFacade() {
        try {
            Context c = new InitialContext();
            return (AccountFacadeLocal) c.lookup("java:comp/env/AccountFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

}
